package org.rasterfun

import java.io.{InputStreamReader, InputStream}
import java.util.Properties

/**
 * A commandline application for rendering Rasterfun rasters and objects.
 *
 * @author Hans Haggstrom
 */

object RasterfunCommandline {


  // Get often changing and potentially changing data from property file
  // (things like version numbers and build numbers)
  val properties = loadProperties("application.properties")

  val APPLICATION_NAME = properties.getProperty("applicationName", "Rasterfun")
  val VERSION = properties.getProperty("version", "N/A")
  val STATUS = properties.getProperty("status", "")
  val VERSION_AND_STATUS = VERSION + " " + STATUS
  val REPOSITORY_VERSION = properties.getProperty("repositoryVersion", "N/A")
  val RELEASE_DATE = properties.getProperty("releaseDate", "N/A")
  val HOMEPAGE_PRETTY = properties.getProperty("homepage", "www.rasterfun.org")
  val TAG_LINE = properties.getProperty("oneLineDescription", "")
  val CREDITS = properties.getProperty("credits", "Programmed by Hans Häggström ( zzorn @ iki.fi )")
  val LICENSE = properties.getProperty("license", "LGPL")
  val BUG_REPORT_URL = properties.getProperty("bugReportUrl", "")
  val FEATURE_REQUEST_URL = properties.getProperty("featureRequestUrl", "")

  val HOMEPAGE_URL = "http://" + HOMEPAGE_PRETTY
  val NAME_AND_VERSION = APPLICATION_NAME + " " + VERSION_AND_STATUS
  val NAME_VERSION_AND_DATE = APPLICATION_NAME + " v. " + VERSION_AND_STATUS + " (revision " + REPOSITORY_VERSION + "), released " + RELEASE_DATE


  def main(args: Array[String]) {

    // TODO: Parse command line options

    // TODO: Set up variables, load input(s), and produce output(s)

    printHelp()
  }


  private def printHelp() {
    println(NAME_VERSION_AND_DATE)
    println(TAG_LINE)
    println()

    // TODO: Usage instructions
  }


  private def loadProperties(resourcePath: String): Properties = {

    def loadResource[T](resourcePath: String, resourceDesc: String, loader: (InputStream) => T, default: => T): T = {
      // TODO: Could return a future maybe - requires a spawn type future support for scala (scalax.Future seems promising)
      // But then it should probably get the concrete default object.

      try
      {
        val classloader: ClassLoader = RasterfunCommandline.getClass().getClassLoader()
        return loader(classloader.getResourceAsStream(resourcePath))
      }
      catch
      {
        case e: Exception => {
          val defaultVal = default

          // TODO: Use logging
          System.err.println("Failed to load the resource '" + resourceDesc + "' " +
                  "from the location '" + resourcePath + "' on the classpath" +
                  (if (defaultVal != null) " (using default value \""+defaultVal+"\" instead)" else "") + " : " + e.toString())
          e.printStackTrace

          return defaultVal
        }
      }
    }

    val properties = new Properties()

    loadResource[Properties](resourcePath, "Properties", (stream) => {
      properties.load(new InputStreamReader(stream))
      properties
    }, properties)
  }


}